// pages/home/clockIn/clockIn.js
import {
  ClockIn
} from 'clockIn-model.js';
import {
  Config
} from '../../../utils/config.js';
var util = require('../../../utils/util.js');
var Function = require("../../../utils/function.js");
var api = new ClockIn(); //实例化 首页 对象
var getUserInfo = require('../../auth/auth.js')
var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //详情
    details: false,

    he: null,
    sind: 0,
    //倒计时
    js: '',
    time: '',
    he: 100,
    px: 0,
    punch_type: 3,
    scale: '',
    show: false, //是否显示弹框
    is_wifi:false,//是否连接wifi
    wifi_BSSID:'',//wifi的bssid
    wifi_SSID:'',//wifi的ssid
    is_external:2,//是否外勤打卡
    area:false,//用户打卡区域信息
    date:''
  },

  //打卡信息
  bindSign() {
    let date = this.data.date;
    api.getSign({ date: date, is_log: 1 }, (res) => {
      if (res.code == 0) {
        App.showError(res.msg, function () {
          wx.navigateBack();
        });
      } else if (res.code == 1){
        wx.getLocation({
          type: 'gcj02',
          success: (res) => {
            var latitude = res.latitude;
            var longitude = res.longitude;
            console.log('res',res)
            this.setData({
              latitude: res.latitude,
              longitude: res.longitude
            });

            api.getSign({
              date: date,
              is_log: 0,
              latitude: this.data.latitude ? this.data.latitude : 0,
              longitude: this.data.longitude ? this.data.longitude : 0
            }, (res) => {
              this.setData({
                details: res.data,
                punch_type: res.data.punch_type,
                px: this.data.he * res.data.punch.length - this.data.he,
                date: res.data.date,
                is_external:res.data.is_external,
                area:res.data.area
              })
            })
          },
          fail:(res)=>{
            App.showError('请打开位置信息', function () {
              wx.navigateBack();
            });
          }
        })
      }else if(res.code == 2){
        App.showError(res.msg, function () {
          wx.navigateBack();
        });
      } 
    })
  },


  
  //获取wifi的mac
  getMac(){
    let that = this;
    wx.startWifi({
      success(res) {
        console.log(res.errMsg, 'wifi初始化成功')
        console.log(res, 'wifi初始化成功2')
      },
      fail: function(res){
        console.log(res.errMsg, 'wifi初始化失败')
      }
    })
    wx.getConnectedWifi({
      success: function(e){
        console.log(e.wifi, 'wifi获取成功')
        
        that.setData({
          wifi_SSID: e.wifi.SSID,
          wifi_BSSID: e.wifi.BSSID,
          is_wifi:true
        })
      },
      fail: function(e){
        console.log(e, 'wifi获取失败')
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin()
    });
    if (_this.data.isLogin === false) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.bindSign();
    this.getMac();
    //倒计时
    this.data.timer = setInterval(() => {
      let time = util.formatDate(new Date());
      this.setData({
        time: time
      })
    }, 1000)
  },
  // 打卡
  punch() {
    //先获取当前到位置
    if (this.data.latitude) {

      //是否连接wifi
      if(!this.data.is_wifi){
        Function.layer('打卡前需连接指定wifi')
        return false;
      }

      //连接wifi是否是指定的
      if(this.data.wifi_BSSID != this.data.area.mac){
        console.log(this.data.area,'mac地址有误');
        Function.layer('非指定区域wifi');
        return false;
      }

      api.postSign({
        // address: '',
        x_axis: this.data.longitude,
        y_axis: this.data.latitude,
        time_types: this.data.details.punch_type,
        wifiname:this.data.wifi_SSID,
        wifimac:this.data.wifi_BSSID,
        is_external:this.data.is_external
      }, (res) => {
        if (res.code == 1) {
          this.bindSign();
          this.setData({
            show: true,
            scale: 'free_download',
          })

        }else if(res.code == 200){
          this.bindSign();
        }else {

          Function.layer(res.msg)
        }
      })
    }
  },
  //确定
  determine() {
    this.setData({
      scale: 'free_upload',
      show: false
    })
  },
  // 选择时间
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    this.bindSign();
  },

  linkTo(e) {
    let url = Function.getDataSet(e, 'url');
    Function.linkTo(url)
  },

  onHide() {
    clearInterval(this.data.time)
    this.setData({ time : false })
  },
})